﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TFSCodeReviewHelper
{
    /// <summary>
    /// Interaction logic for AssetGrid.xaml
    /// </summary>
    public partial class AssetGrid : UserControl
    {
        private WorkItemChanges _WorkItemChanges;
        public WorkItemChanges WorkItemChanges
        {
            get { return _WorkItemChanges; }
            set
            {
                _WorkItemChanges = value;
                _listView.ItemsSource = value.Information;
                BuildCustomHeader();
            }
        }
        public AssetGrid()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            BuildCustomHeader();
        }

        protected void BuildCustomHeader()
        {
            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;

            if (WorkItemChanges != null)
            {
                foreach (AssetChange change in WorkItemChanges.Changes)
                {
                    TextBlock block = new TextBlock();
                    block.Width = 68;
                    block.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    block.Text = change.Number.ToString();
                    block.TextAlignment = TextAlignment.Center;
                    block.Text += "\r\n";
                    block.Text += change.Date.ToShortDateString();
                    panel.Children.Add(block);

                    block = new TextBlock();
                    block.Width = 12;
                    panel.Children.Add(block);
                }
            }
            _gridView.Columns[_gridView.Columns.Count - 1].Header = panel;
        }

        private void AssetLocationTextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
               
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            ListViewItem listViewItem = _listView.ItemContainerGenerator.ContainerFromItem(button.DataContext) as ListViewItem;

            AssetInformation information = listViewItem.Content as AssetInformation;

            information.Reset();
        }

        private void ChangesetParticiationControl_ChangeSelected(object sender, ChangeSelectedArgs e)
        {
            ChangesetParticiationControl control = sender as ChangesetParticiationControl;
            ListViewItem listViewItem = _listView.ItemContainerGenerator.ContainerFromItem(control.DataContext) as ListViewItem;
            AssetInformation information = listViewItem.Content as AssetInformation;
            information.OnChangeSelected(control, e);
        }


    }
}
